﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using MPI;

namespace Dwarf.Managed
{
    /// <summary>
    /// User interface for solving the Graph traversal Problem.
    /// </summary>
    class Program
    {
        /// <summary>
        /// The settings for this dwarf.
        /// </summary>
        static private Settings settings;

        /// <summary>
        /// Get settings from the command line.
        /// </summary>
        /// <param name="args">
        /// Cli params.
        /// </param>
        /// <param name="solver">
        /// Instance of Solver.
        /// </param>
        static void getSettings(String[] args, Solver solver)
        {
            bool error = false;
            if (solver.isRootThread)
            {
                try
                {
                    settings = new Settings();
                    settings.Init(args, solver);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    error = true;
                }
            }
            //Distribute error if that occurred while parsing.
            Communicator.world.Broadcast(ref error, Settings.ROOT_PROCESS);
            if (error) throw new Exception("");

            int length = 0;
            if (solver.isRootThread)
            {
                length = solver.graph.getLength();
            }
            //Distribute the number of states.
            Communicator.world.Broadcast(ref length, Settings.ROOT_PROCESS);

            if (!solver.isRootThread)
            {
                solver.graph = new Graph(length);
            }

            //Distribute the matrix of edges 
            for (int i = 0; i < length; i++)
            {   
                int lengthMatr = 0;
                if(solver.isRootThread)
                {
                    lengthMatr = solver.graph.edgesMatrix[i].Length;
                }

                Communicator.world.Broadcast(ref lengthMatr, Settings.ROOT_PROCESS);

                if (!solver.isRootThread)
                {
                    solver.graph.edgesMatrix[i] = new int[lengthMatr];
                }
                Communicator.world.Broadcast(ref solver.graph.edgesMatrix[i], Settings.ROOT_PROCESS);
            }

        }

        /// <summary>
        /// Point of the program start.
        /// </summary>
        /// <param name="args">
        /// Cli params.
        /// </param>
        static void Main(String[] args)
        {
            
            try
            {
                using (new MPI.Environment(ref args))                       // MPI Initialization.
                {
                    if (Communicator.world.Size == 1)
                    {
                        throw new Exception("Only one mpi process.");
                    }

                    Solver solver = new Solver();

                    getSettings(args, solver);    // Get settings from the command line.

                    Communicator.world.Barrier();
                    if (Communicator.world.Rank == Settings.ROOT_PROCESS)
                        settings.Start();             // Start new time count.
                    solver.Solve();                   // Solve the current problem.
                    Communicator.world.Barrier();
                    if (Communicator.world.Rank == Settings.ROOT_PROCESS)
                        settings.Finish(solver);      // Stop the time count and write results.

                    solver.GatherResults();           // Gather on root processor.
 
                    if (Communicator.world.Rank == Settings.ROOT_PROCESS)
                        settings.WriteResults(solver);

                }
        
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //usage();
            }

        }
    }
}
